﻿using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Threading;
using static HomagChina.CCHCS.ProcessResourceInternal;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class TaskInfoViewModel : NotifyPropertyChangedBase
    {
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }
        public RelayCommand ResetCommand { get; }
        public RelayCommand CompleteCommand { get; }

        private string JobId { get; set; }
        private string StackId { get; set; }

        private ObservableCollection<TaskInfoModel> _List_TaskInfoModel;

        public ObservableCollection<TaskInfoModel> List_TaskInfoModel
        {
            get => _List_TaskInfoModel;
            set { _List_TaskInfoModel = value; OnPropertyChanged(); }
        }

        private DispatcherTimer UpdateSessionTimer;

        public event EventHandler RequestScrollPositionSave;

        public event EventHandler RequestScrollPositionRestore;

        public IDictionary<int, Brush> CustomStatus { get; set; }

        public TaskInfoViewModel(JobModel model)
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);
            ResetCommand = new RelayCommand(Reset);
            CompleteCommand = new RelayCommand(Complete);
            JobId = model.JobId;
            StackId = model.StackId;
            CustomStatus = new Dictionary<int, Brush>
            {
                { 0, GetWindowResouceBrush(HomagGroup.Base.UI.Themes.Resources.Keys.MoreImage) },
                { 10, GetWindowResouceBrush(HomagGroup.Base.UI.Themes.Resources.Keys.MoreImage) },
                { 20, GetWindowResouceBrush(HomagGroup.Base.UI.Themes.Resources.InternalKeys.TableActualStateImage) },
                { 30, GetWindowResouceBrush(HomagGroup.Base.UI.Themes.Resources.InternalKeys.TableFinishedStateImage) }
            };
            Quary(JobId, StackId);
        }

        /// <summary>
        ///
        /// </summary>
        private void ViewloadedCommand()
        {
            UpdateSessionTimer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(5000)
            };
            UpdateSessionTimer.Tick += UpdateSessionTimer_Tick;
            UpdateSessionTimer.Start();
        }

        private void UpdateSessionTimer_Tick(object sender, EventArgs e)
        {
            RequestScrollPositionSave?.Invoke(this, EventArgs.Empty);
            Quary(JobId, StackId);
            RequestScrollPositionRestore?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        /// Unloaded event
        /// </summary>
        private void ViewUnloadedCommand()
        {
            UpdateSessionTimer.Stop();
        }

        public void Quary(string JobId, string StackId)
        {
            List_TaskInfoModel = Handle_Search.GetTaskInfoModels(JobId, StackId);
        }

        public void Reset(object SelectItem)
        {
            var info = (SelectItem as TaskInfoModel);
            if (info.TransferState.Equals(30))
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Cannot be reset"));
                return;
            }
            Handle_Modify.ModifyTaskState(info.Id, 0);
            Quary(JobId, StackId);
        }

        public void Complete(object SelectItem)
        {
            var info = (SelectItem as TaskInfoModel);
            if (info.TransferState.Equals(30))
            {
                return;
            }
            Handle_Modify.ModifyTaskState(info.Id, 30);
            Quary(JobId, StackId);
        }
    }
}